package app.softtech.freechat.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import app.softtech.freechat.R;
import app.softtech.freechat.app.AppController;
import app.softtech.freechat.database.DatabaseFriend;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;

public class CustomListViewAdapter extends BaseAdapter {
	//Get current activity
	private Activity mActivity;
	private LayoutInflater mInflater;
	private List<DatabaseFriend> mFiendList;
	ImageLoader imageLoader = AppController.getInstance().getImageLoader();
	
	static final String TAG = CustomListViewAdapter.class.getSimpleName();
	
	
	public CustomListViewAdapter(Activity activity, List<DatabaseFriend> friendList){
		this.mActivity = activity;
		this.mFiendList = friendList;
	}
	
	@Override
	public int getCount() {
		return mFiendList.size();
	}

	@Override
	public Object getItem(int position) {
		return mFiendList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(mInflater == null){
			//Initialized the inflater if null
			mInflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		if(convertView == null){
			//Initialized the convert view if null
			convertView = mInflater.inflate(R.layout.list_row, parent, false);
		}
		
		if(imageLoader == null){
			//Initialized the image loader if null
			imageLoader = AppController.getInstance().getImageLoader();
		}
		
		//Get attributes
		NetworkImageView friendAvatar = (NetworkImageView) convertView.findViewById(R.id.img_avatar);
		TextView friendName = (TextView) convertView.findViewById(R.id.tv_friend_name);
		TextView friendStatus = (TextView) convertView.findViewById(R.id.tv_friend_status);
		
		//Get database of friend by position
		DatabaseFriend df = mFiendList.get(position);
		
		//Add database to attributes
		friendAvatar.setImageUrl(df.getFriend_Avatar(), imageLoader);
		friendName.setText(df.getFriend_Name());
		friendStatus.setText(df.getFriend_Status());
		
		return convertView;
	}

}
